package com.ld.security.auth.common.util;

import com.ld.security.auth.common.base.entity.JwtUser;
import com.ld.security.auth.common.base.entity.JwtUserInfo;
import com.ld.security.common.core.base.column.ListString;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.mybatis.core.entity.SqlDeptEntity;
import com.ld.security.common.mybatis.core.entity.SqlUserEntity;
import com.ld.security.common.shiro.constants.ShiroCacheConstants;
import com.ld.security.common.shiro.util.SessionUtil;
import com.ld.security.common.shiro.vo.UserInfo;

/**
 * 认证工具
 */
public class AuthUtil {

    public static UserInfo getUserInfo(){
        return SessionUtil.get(ShiroCacheConstants.userKey);
    }

    public static void initEntity(Object entity){
        JwtUserInfo userInfo = (JwtUserInfo) AuthUtil.getUserInfo();
        if(entity instanceof SqlUserEntity){
            SqlUserEntity userPo = (SqlUserEntity) entity;
            if(ReflectionUtil.isNull( userPo.getCrtUser())){
                userPo.setCrtUser(userInfo.getUser().getId());
            }

        }
        if(entity instanceof SqlDeptEntity){
            SqlDeptEntity deptPo = (SqlDeptEntity) entity;
            if(ReflectionUtil.isNull( deptPo.getCrtDept())){
                deptPo.setCrtDept(userInfo.getUser().getCrtDept());
            }
        }
    }

}
